import { getToken, isNullOrWhitespace, isAdminEnd } from '@/utils'

// guard 控制“认证”
const WHITE_LIST = [
  '/404',
  '/admin',
  '/admin/login',
  '/admin/register',
  '/admin/re-pw-info',
  '/admin/reset-password',
]
export function createPermissionGuard(router) {
  router.beforeEach(async (to) => {
    const token = getToken()

    /** 没有token的情况 */
    if (isNullOrWhitespace(token)) {
      if (WHITE_LIST.includes(to.path)) return true
      // 提示登录
      $message.info('请先登录！')
      return {
        name: 'AdminLogin',
        query: { ...to.query, redirect: to.path },
      }
    }

    return true
  })
}
